using System;

namespace atm
{
    public class ATM
    {
        public string Location { get; init; }
        public Bank Bank { get; init; }
        
        public delegate void BigMoneyFetchedEventHandler(object sender, BigMoneyArgs e);
        public event BigMoneyFetchedEventHandler BigMoneyFetched;

        private Random random = new Random();

        public ATM(string location, Bank bank)
        {
            Location = location;
            Bank = bank;
        }
        
        public void Withdraw(Account account, decimal amount)
        {
            try
            {
                if (random.Next(0, 100) < 30)
                {
                    throw new BadCashException("检测到破损钞票");
                }

                account.Withdraw(amount);
                Console.WriteLine($"{account.Name} 取走了 {amount} 元，当前余额: {account.Balance}");
                
                if (amount > 10000)
                {
                    OnBigMoneyFetched(new BigMoneyArgs(account, amount));
                }
            }
            catch (BadCashException ex)
            {
                Console.WriteLine($"错误: {ex.Message}");
            }
        }
        
        protected virtual void OnBigMoneyFetched(BigMoneyArgs e)
        {
            BigMoneyFetched?.Invoke(this, e);
        }
    }
}